﻿using IWshRuntimeLibrary;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;

namespace DesktopLauncher.Helper
{
    /// <summary>
    /// 获取图标的公共类
    /// </summary>
    public class IconHelper
    {
        /// <summary>
        /// 存放exe和lnk之外类型的图标,第二次使用就不用再次初始化了.
        /// </summary>
        protected Dictionary<string, Icon> iconCache = new Dictionary<string, Icon>();

        public static Icon DefaultIcon
        {
            get { return Properties.Resources.ICO_DefaultApp; }
        }
        public static Icon ExitIcon
        {
            get { return Properties.Resources.ICO_Exit; }
        }
        public static Icon SettingIcon
        {
            get { return Properties.Resources.ICO_Config; }
        }




        public Icon GetfileExtIcon(IntPtr h, string fileFullPath)
        {
            string fileExt = Path.GetExtension(fileFullPath);
            if (iconCache.ContainsKey(fileExt))
            {
                return iconCache[fileExt];
            }
            Icon icon = null;
            if (fileExt.Equals(".exe") || fileExt.Equals(".lnk"))
            {
                string fullPath = fileFullPath;
                if (fileExt.Equals(".lnk"))
                {
                    IWshShortcut _shortcut = null;
                    IWshShell_Class shell = new IWshShell_Class();
                    if (System.IO.File.Exists(fileFullPath) == true)
                        _shortcut = shell.CreateShortcut(fileFullPath) as IWshShortcut;//在vs2010中CreateShortcut返回dynamic 类型,所以要加as 进行对象类型转换  
                    fullPath = _shortcut.TargetPath;
                }
                try
                {
                    icon = Icon.ExtractAssociatedIcon(fullPath);//getExtractIcon(h, fullPath, 0);
                }
                catch (FileNotFoundException ex)
                {
                    icon = null;
                }
            }
            else
            {
                RegistryKey regRead = Registry.ClassesRoot.OpenSubKey(fileExt);
                if (regRead == null)
                    return DefaultIcon;
                string subKey = regRead.GetValue("").ToString();
                RegistryKey regRead1 = Registry.ClassesRoot.OpenSubKey(subKey);
                if (regRead1 == null)
                    return DefaultIcon;
                RegistryKey subKey1 = regRead1.OpenSubKey("DefaultIcon");
                if (subKey1 == null)
                    return DefaultIcon;
                string defaultIcon = subKey1.GetValue("").ToString();
                string[] defIcon = defaultIcon.Split(',');
                icon = getExtractIcon(h, defIcon[0], int.Parse(defIcon[1]));

                if (icon == null)
                    icon = DefaultIcon;

                iconCache.Add(fileExt, icon);
            }

            if (icon == null)
                icon = DefaultIcon;
            return icon;
        }

        //获取系统图标
        [DllImport("shell32.dll")]
        private static extern int ExtractIcon(IntPtr h, string strx, int ii);
        private static Icon getExtractIcon(IntPtr h, string fileName, int iIndex)
        {
            IntPtr hIcon = (IntPtr)ExtractIcon(h, fileName, iIndex);
            if (hIcon != IntPtr.Zero)
            {
                Icon icon = Icon.FromHandle(hIcon);
                return icon;
            }
            return null;
        }
    }
}
